﻿using System;
using System.Text.RegularExpressions;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Resources.Definitions
{
  internal interface INamingRule
  {
    String Pattern { get; }
    Boolean IsSatisfiedBy(String name);
  }

  internal class NamingRule : INamingRule
  {
    private String _namingPattern;
    private Regex _namingRegex;

    public String Pattern { get { return _namingPattern; } }

    public NamingRule(String namingPattern)
    {
      Verify.NotWhiteSpace(namingPattern, "namingPattern");
      
      _namingPattern = namingPattern;
      _namingRegex = new Regex(namingPattern, RegexOptions.Singleline);
    }

    public Boolean IsSatisfiedBy(String name)
    {
      return !String.IsNullOrEmpty(name) && _namingRegex.IsMatch(name);
    }
  }

  internal static class NamingPattern
  {
    /* Note: 
     * http://www.w3schools.com/tags/att_standard_id.asp
     * 
     * Naming rules:
     * - Must begin with a letter A-Z or a-z
     * - Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
     * - Values are case-sensitive
     */
    public const String HtmlIdAttribute = @"^[a-zA-Z]+[_a-zA-Z0-9-\.]*$";

    /* Note: 
     * http://www.w3.org/TR/CSS21/syndata.html
     * 
     * 4.1.2.1 Vendor-specific extensions
     * "identifiers may begin with '-' (dash) or '_' (underscore). Keywords and property names beginning with -' or '_' are reserved for vendor-specific extensions."
     * 
     * In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, 
     * plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any 
     * ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F". 
     * 
     * ISO 10646 characters U+00A1 and higher NOT HANDLED
     */
    public const String CssClass = @"^[a-zA-Z]+[_a-zA-Z0-9-]*$";
  }
}
